//
//  AdvDetailViewController.m
//  liwushuo
//
//  Created by qf1 on 16/4/15.
//  Copyright (c) 2016年 ZJH. All rights reserved.
//

#import "AdvDetailViewController.h"
#import "DetailViewController.h"

@interface AdvDetailViewController ()<UITableViewDelegate,UITableViewDataSource,WKNavigationDelegate,WKUIDelegate>

@end

@implementation AdvDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.translucent = NO;
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back"] style:UIBarButtonItemStylePlain target:self action:@selector(backAction)];
    MBProgressHUD * hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.labelText = @"正在加载";
    hud.detailsLabelText = @"请稍后";
    [hud show:YES];
    
    _dataArray = [[NSMutableArray alloc] init];
    
    [self createView];
}
- (void)backAction{
        [self.navigationController popViewControllerAnimated:YES];
    
}
- (void)createView{
//   有时候会有不同类型的内容
    NSRange range = [self.redirect_value rangeOfString:@"topic_id="];
    if (range.length != 0) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, theScreenWidth, theScreenHeight - 64) style:UITableViewStylePlain];
        _tableView.rowHeight = theScreenWidth * 2 / 5;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_tableView registerNib:[UINib nibWithNibName:@"GuideContentTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
        [self.view addSubview:_tableView];
        [self getData];
        }
    else{
//     WKWebViewConfiguration * configuration = [[WKWebViewConfiguration alloc] init];
//     _webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, theScreenWidth, theScreenHeight - 64) configuration:configuration];
//     _webView.navigationDelegate = self;
//     _webView.UIDelegate = self;
//     [_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:_advItem.target_url]]];
//     [self.view addSubview:_webView];
     }
    
}

- (void)getData{
    NetWorkManager * manager = [[NetWorkManager alloc] init];
    __weak AdvDetailViewController * weakself = self;
    [manager getAdvContentDataWithUrl:[NSString stringWithFormat:@"http://api.liwushuo.com/v2/collections/%@/posts?limit=20&offset=0",_Id] andBlock:^(id respond) {
        [_dataArray addObjectsFromArray:respond];
        [MBProgressHUD hideAllHUDsForView:weakself.view animated:YES];
        [_tableView reloadData];
    }];
}


//- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation{
//    [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
//}


#pragma mark 列表的协议方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [_dataArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString * cellid = @"cell";
    ContentItem * item = _dataArray[indexPath.row];
    GuideContentTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellid];
    [cell.backGroundImage setImageWithURL:[NSURL URLWithString:item.cover_image_url]];
    CALayer * layer = cell.backGroundImage.layer;
    layer.cornerRadius = 5;
    layer.masksToBounds = YES;
    cell.title.text = item.title;
    cell.favouriteLabel.text = item.likes_count;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    ContentItem * item = _dataArray[indexPath.row];
    DetailViewController * vc = [[DetailViewController alloc] init];
    [self.navigationController pushViewController:vc animated:NO];
    vc.Id = item.Id;
    
}

- (void)viewWillAppear:(BOOL)animated{
    
    [self.tabBarController.tabBar setHidden:YES];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
